package main

import (
	"fmt"
	"reflect"
	"strings"
)

type order struct {
	ordId      int
	customerId int
}

type employee struct {
	name    string
	id      int
	address string
	salary  float64
	country string
	isManager bool
}

func main() {
	o := order{
		ordId:      456,
		customerId: 56,
	}
	createQuery(o)

	e := employee{
		name:    "Naveen",
		id:      565,
		address: "Coimbatore",
		salary:  90000.50,
		country: "India",
		isManager: false,
	}
	createQuery(e)
	i := 90
	createQuery(i)
}

func createQuery(q interface{}) {
	value := reflect.ValueOf(q)
	if reflect.Struct != value.Kind() {
		fmt.Printf("unsupported type:%s\n", value.Kind())
		return
	}

	var values []string
	tableName := value.Type().Name()
	for i := 0; i < value.NumField(); i++ {
		field := value.Type().Field(i)
		fieldName := field.Name
		fieldValue := value.FieldByName(fieldName)
		if fieldValue.Kind() == reflect.String {
			values = append(values, fmt.Sprintf("'%s'", fieldValue.String()))
		} else {
			values = append(values, fmt.Sprint(fieldValue))
		}
	}
	sql := fmt.Sprintf("insert into %s values(%s)", tableName, strings.Join(values, string(',')))
	fmt.Println(sql)
}
